//
//  AssistantDetailController.m
//  云律司
//
//  Created by 韦淋 on 2017/8/2.
//  Copyright © 2017年 test. All rights reserved.
//

#import "AssistantDetailController.h"

@interface AssistantDetailController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    UITextField *searchField;
    UILabel *searchType;
    UIButton *searchButton;
    NSString *searchString;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;



@end

@implementation AssistantDetailController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    //    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    self.barTitleLable.text = @"法律列表";
    [self initUI];
    [self getDataSource];
}
- (void)initUI{
    [self.view insertSubview:self.tableView atIndex:0];
}
- (void)getDataSource{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@cons_type=%@",LAWS_URL ,self.lawType] dic:nil Succeed:^(id responseData) {
        
        self.dataSource = responseData[@"result"];
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@",self.dataSource);
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark - UITextFieldDelegate

//- ( BOOL )textField:( UITextField  *)textField shouldChangeCharactersInRange:(NSRange )range replacementString:( NSString  *)string{
//    searchString = textField.text;
//    return YES;
//}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.clearsOnBeginEditing = YES;
    searchButton.backgroundColor = [UIColor lightGrayColor];
    searchButton.userInteractionEnabled = NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    searchString = textField.text;
    if (textField.text.length == 0) {
        searchButton.backgroundColor = [UIColor lightGrayColor];
        searchButton.userInteractionEnabled = NO;
    }
    else{
        searchButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        searchButton.userInteractionEnabled = YES;
    }
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    searchString = textField.text;
    if (textField.text.length == 0) {
        searchButton.backgroundColor = [UIColor lightGrayColor];
        searchButton.userInteractionEnabled = NO;
    }
    else{
        searchButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        searchButton.userInteractionEnabled = YES;
    }
}
#pragma mark - ButtonTarget
- (void)searchAction:(UIButton *)sender{
    NSLog(@"查询 ");
    if (searchString.length == 0) {
        [MBProgressHUD showError:@"请输入搜索内容"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    
    NSLog(@"cons_type : %@",self.lawType);
    NSLog(@"cons_name : %@",searchString);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager getUrl:SEARCHLAWS_URL dic:@{@"cons_type":self.lawType,@"cons_name":searchString} Succeed:^(id responseData) {
        self.dataSource = responseData[@"result"];
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"success = %@",responseData);
    } failure:^(NSError *error) {
        NSLog(@"error = %@",error);
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AssistantLawCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AssistantLawCell" forIndexPath:indexPath];
    
    cell.titleLable.text = self.dataSource[indexPath.row][@"cons_name"];
    cell.applyDate.text = [NSString stringWithFormat:@"施行日期： %@",self.dataSource[indexPath.row][@"perform_date"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    
    UIView *searchTextView = [[UIView alloc] initWithFrame:CGRectMake(30, 5, 200, 30)];
    searchTextView.layer.borderColor = [UIColor colorWithRed:102.0f/255.0f green:159.0f/255.0f blue:190.0f/255.0f alpha:1.0f].CGColor;
    searchTextView.layer.borderWidth = 1.0f;
    searchTextView.layer.cornerRadius = 5;
    searchTextView.layer.masksToBounds = YES;
    searchTextView.backgroundColor = [UIColor whiteColor];
    
    
    searchField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, 120, 30)];
    searchField.delegate = self;
    searchString = searchField.text;
    
    
    
    searchButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 5, 100, 30)];
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    searchButton.layer.cornerRadius = 5;
    searchButton.layer.masksToBounds = YES;
    searchButton.backgroundColor = [UIColor lightGrayColor];
    searchButton.userInteractionEnabled = NO;
    
    [headerView addSubview:searchTextView];
    [searchTextView addSubview:searchField];
    [headerView addSubview:searchButton];
    
    
    return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

#pragma mark - Getter

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 180;
        [_tableView registerClass:[AssistantLawCell class] forCellReuseIdentifier:@"AssistantLawCell"];
        
        
    }
    return _tableView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
